/////////////////////////////////////////////////////////////////////////
//
// Amuse Engine SDK - core/threads
// Copyright (c) 2014.  All Rights Reserved
//
// File:		AEEvent_win32.h
// Author:		Gianluca Belardelli
// Date:		12/03/2014
//
/////////////////////////////////////////////////////////////////////////
#ifndef WIN32
#error "Errore! Il file contiene codice specifico per Win32"
#endif

#include <AECore.h>

AEEvent::AEEvent( AEBOOL32 bAutoReset, AEBOOL32 bInitialState )
{
	m_hEvent = CreateEventW( NULL, bAutoReset ? FALSE : TRUE, bInitialState ? TRUE : FALSE, NULL );
}

AEEvent::~AEEvent( void )
{
	if( m_hEvent )
		CloseHandle( m_hEvent );
}

AEINT32 AEEvent::Set( void )
{
	if( !SetEvent( m_hEvent ) )
		return AEEvent_SetError;

	return AEEvent_Ok;
}

AEINT32 AEEvent::WaitInfinite( void )
{
	switch( WaitForSingleObject( m_hEvent, INFINITE ) )
	{
		case WAIT_OBJECT_0:
			return AEEvent_Ok;
		default:
			return AEEvent_WaitError;
	}
}

AEINT32 AEEvent::Wait( AEUINT32 uiMilliseconds )
{
	switch( WaitForSingleObject( m_hEvent, uiMilliseconds ) )
	{
		case WAIT_OBJECT_0:
			return AEEvent_Ok;
		case WAIT_TIMEOUT:
			return AEEvent_Timeout;
		default:
			return AEEvent_WaitError;
	}
}

AEINT32 AEEvent::TryWait( AEUINT32 uiMilliseconds )
{
	switch( WaitForSingleObject( m_hEvent, uiMilliseconds ) )
	{
		case WAIT_OBJECT_0:
			return AEEvent_Ok;
		case WAIT_TIMEOUT:
			return AEEvent_Timeout;
		default:
			return AEEvent_WaitError;
	}
}

AEINT32 AEEvent::Reset( void )
{
	if( !ResetEvent( m_hEvent ) )
		return AEEvent_SetError;

	return AEEvent_Ok;
}
